package com.juhuacha.tryrxjava.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.juhuacha.tryrxjava.R;

/**
 * Created by Administrator on 2016/8/29.
 * 邮箱       juhuacha18@gmail.com
 * E-Mail     juhuacha18@gmail.com
 * CopyRight  9hgame.com reserved
 */
public class CustomCircle extends View {

    private int length;
    private int mCircleXy;
    private float mRadius;
    private RectF mArcRectF;
    private Paint mTextPaint;

    public CustomCircle(Context context) {
        this(context,null,0);
    }

    public CustomCircle(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        length = getMeasuredWidth();
        mCircleXy = length / 2;
        mRadius = (float)(length * 0.5 / 2);
        mArcRectF = new RectF(
                (float)(length*0.1),
                (float)(length*0.1),
                (float)(length*0.9),
                (float)(length*0.9)
        );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint mTextPaint =  new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(getResources().getColor(R.color.white));

        Paint mArcPaint =  new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setColor(getResources().getColor(R.color.colorPrimary));
        mArcPaint.setStrokeWidth((float) 10.0);              //线宽
        mArcPaint.setStyle(Paint.Style.STROKE);

        Paint mCirclePaint =  new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(getResources().getColor(R.color.colorAccent));


        canvas.drawCircle(mCircleXy,mCircleXy,mRadius,mCirclePaint);
        canvas.drawArc(mArcRectF,270,240,false,mArcPaint);
        canvas.drawText("绘制测试",0,4,mCircleXy - mTextPaint.getTextSize()*4/2,mCircleXy,mTextPaint);
    }
}
